package net.xrkw.server.module.client.service.index;

import net.xrkw.server.module.client.dal.dataobject.softcategory.SoftCategoryDO;
import net.xrkw.server.module.client.dal.dataobject.softinfo.SoftInfoDO;
import net.xrkw.server.module.client.dal.mysql.softcategory.SoftCategoryMapper;
import net.xrkw.server.module.client.dal.mysql.softinfo.SoftInfoMapper;
import net.xrkw.server.module.client.service.softcategory.SoftCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private SoftInfoMapper softInfoMapper;
    @Resource
    private SoftCategoryMapper softCategoryMapper;

    @Override
    public void getIndexData(Model model) {
        List<SoftCategoryDO> categories = softCategoryMapper.selectList();
        model.addAttribute("categories", categories);

        // 预查询每个分类的软件列表
        Map<Long, List<SoftInfoDO>> softsByCategory = categories.stream()
                .collect(Collectors.toMap(
                        SoftCategoryDO::getId,
                        category -> softInfoMapper.selectList(SoftInfoDO::getCategoryId, category.getId())
                ));
        model.addAttribute("softsByCategory", softsByCategory);
    }

    @Override
    public void getSoftDetailData(Long id, Model model) {
        model.addAttribute("soft", softInfoMapper.selectById(id));
    }
}
